package com.example.mongo.service;

import com.example.mongo.Model.bean.Chapter;
import com.example.mongo.Model.bean.Menu;
import com.example.mongo.Model.bean.User;
import com.example.mongo.Model.dto.ComicItemDto;
import com.example.mongo.dao.ChapterMapper;
import com.example.mongo.dao.UserMapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@CacheConfig(cacheNames = "chapter")
@Service
public class ChapterService {

    @Autowired
    public ChapterMapper chapterMapper;
    @Autowired
    public MenuService menuService;

    @Cacheable
    public List<Chapter> getChapterByComicId(int comicId) {
        return chapterMapper.getChapterByComicId(comicId);
    }

    public Chapter getChapterByChapterId(int chapterId) {
        return chapterMapper.getChapterByChapterId(chapterId);
    }

    public ComicItemDto getComicByComicId(int comicId) {
        ComicItemDto comicItemDto =null;
        Menu menu = menuService.getMenuByComicId(comicId);
        List<Chapter> list = chapterMapper.getChapterByComicId(comicId);
        if (null == menu && null == list) {
            return null;
        }
        comicItemDto = new ComicItemDto();
        comicItemDto.setChapter(JSONArray.fromObject(list).toString());
        comicItemDto.setComic(JSONObject.fromObject(menu).toString());

        return comicItemDto;
    }
}
